package com.zt.student.service.impl;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.zt.common.Do.ClassWithTeachDO;
import com.zt.common.common.ApiException;
import com.zt.common.common.CodeMsg;
import com.zt.common.pojo.ActiveClass;
import com.zt.common.pojo.ClassSelection;
import com.zt.common.pojo.Teachers;
import com.zt.common.pojo.netty.Sender;
import com.zt.common.utils.ClassUtil;
import com.zt.common.utils.JwtUtil;
import com.zt.common.utils.RedisUtil;
import com.zt.student.dao.ClassSelectionDao;
import com.zt.student.dto.SelectClassDTO;
import com.zt.student.service.ActiveClassService;
import com.zt.student.service.ClassSelectionService;
import com.zt.student.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * description:
 * author:zt
 * date:2022-01-02
 */
@Service
@Transactional
public class ClassSelectionServiceImpl implements ClassSelectionService {

    private static final Log log = LogFactory.get();

    @Autowired
    private ClassSelectionDao classSelectionDao;
    @Autowired
    private ActiveClassService activeClassService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void insert(SelectClassDTO selectClassDTO, HttpServletRequest request) {
        String semester = selectClassDTO.getSemester();
        if (!redisUtil.hasKey(semester + "-selectClass")){
            throw new ApiException(CodeMsg.NOT_SELECT_CLASS);
        }
        //根据id查找预选课堂信息
        ActiveClass selectClass = activeClassService.getClassById(selectClassDTO.getActiveClassId());
        log.info("预选课堂：" + selectClass);
        //获取学生已选的选修通选课
        List<ClassWithTeachDO> selectedElectiveClass = activeClassService.getSelectedElectiveClass(semester, request);
        if (selectedElectiveClass.size() >= 7) throw new ApiException(CodeMsg.CLASS_TOO_MUCH);
        //获取学生所有已选课
        List<ClassWithTeachDO> classDTO = activeClassService.getAllSelectedClass(semester, request);
        for (ClassWithTeachDO c : classDTO) {
            if (ClassUtil.isTimeConflict(selectClass.getClassTime(),c.getClassTime())){
                throw new ApiException(CodeMsg.SELECT_CLASS_TIME_CONFLICT);
            }
        }
        //获取正在操作的学生id
        String studentId = JwtUtil.decodedGetID(request.getHeader("authorization"));

        classSelectionDao.insert(new ClassSelection(selectClassDTO.getActiveClassId(),studentId,-1));
    }

    @Override
    public void delete(SelectClassDTO selectClassDTO, HttpServletRequest request) {
        ActiveClass selectClass = activeClassService.getClassById(selectClassDTO.getActiveClassId());
        log.info("预退选课堂：" + selectClass);

        if (courseService.isCompulsory(selectClass.getCourseId())) {
            throw new ApiException(CodeMsg.NON_CANCELLATION);
        }
        if (!redisUtil.hasKey(selectClassDTO.getSemester() + "-selectClass")){
            throw new ApiException(CodeMsg.NOT_SELECT_CLASS);
        }

        //获取正在操作的学生id
        String studentId = JwtUtil.decodedGetID(request.getHeader("authorization"));

        classSelectionDao.delete(new ClassSelection(selectClassDTO.getActiveClassId(),studentId,-1));
    }

    /**
     * @param request 学生id
     * @return 获取该学生上课的所有老师信息
     */
    @Override
    public List<Sender> getSelectedTeacherInfo(HttpServletRequest request,String semester){
        String studentId = JwtUtil.decodedGetID(request.getHeader("authorization"));
        List<Teachers> selectedTeacherInfo = classSelectionDao.getSelectedTeacherInfo(studentId,semester);

        ArrayList<Sender> teachers = new ArrayList<>();
        selectedTeacherInfo.forEach(t -> teachers.add(new Sender(t.getTeacherId(),t.getName(),t.getAvatar())));

        return teachers;
    }

    @Override
    public ClassSelection getByStuIdAndClassId(HttpServletRequest request, int classId) {
        String studentId = JwtUtil.decodedGetID(request.getHeader("authorization"));
        return classSelectionDao.getByStuIdAndClassId(classId,studentId);
    }
}
